package com.wedata.中台服务;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信
 */
public class MessageCenterQw {

    public static void main(String[] args) {

        //addMsgTemplate();
    }

    public static void addMsgTemplate() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/externalcontact/addMsgTemplate";

        //私发
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"chat_type\":\"single\",\"external_userid\":[\"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA\"],\"text\":{\"content\":\"你好，欢迎加入减脂CRM\"},\"sender\":\"YangLin\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":41035,"errmsg":"missing external userid, hint: [1701246802142830313280182], from ip: 47.95.112.14, more info at https://open.work.weixin.qq.com/devtool/query?e=41035","fail_list":[]},"time":1701246802}:682
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","fail_list":[],"msgid":"msgZw2MCQAA2odDOclpW4pJgnQCAZ55xw"},"time":1701248128}:1178

        //发群里
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"chat_type\":\"group\",\"chat_id_list\":[\"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ\"],\"text\":{\"content\":\"你好，欢迎加入减脂CRM!\"},\"sender\":\"YangLin\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","fail_list":[],"msgid":"msgZw2MCQAA4uxBcTS-bW5rP-LFjVIgeA"},"time":1701248434}:1242

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }

    public static void groupchatGet() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/groupchat/get";
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"chat_id\":\"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","group_chat":{"chat_id":"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ","name":"","create_time":1701238362,"member_list":[{"userid":"YangLin","type":1,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""},{"userid":"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""},{"userid":"wmZw2MCQAArx3jAyw-OTjea0TET_AB8A","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""}],"admin_list":[],"owner":"YangLin","member_version":"7b3b5125822cd09d0c1e47358c5d9c6f"}},"time":1701246339}:817
        //改了一个群名
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","group_chat":{"chat_id":"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ","name":"我们都是好朋友","create_time":1701238362,"member_list":[{"userid":"YangLin","type":1,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""},{"userid":"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""},{"userid":"wmZw2MCQAArx3jAyw-OTjea0TET_AB8A","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":""}],"admin_list":[],"owner":"YangLin","member_version":"7b3b5125822cd09d0c1e47358c5d9c6f"}},"time":1701246442}:871
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"chat_id\":\"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ\",\"need_name\":1}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","group_chat":{"chat_id":"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ","name":"我们都是好朋友","create_time":1701238362,"member_list":[{"userid":"YangLin","type":1,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":"","name":"杨林"},{"userid":"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":"","name":"芝麻沉"},{"userid":"wmZw2MCQAArx3jAyw-OTjea0TET_AB8A","type":2,"join_time":1701238362,"join_scene":1,"invitor":{"userid":"YangLin"},"group_nickname":"","name":"Mr.先生"}],"admin_list":[],"owner":"YangLin","member_version":"7b3b5125822cd09d0c1e47358c5d9c6f"}},"time":1701246576}:813

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }

    public static void groupchatList() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/groupchat/list";
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"limit\":11}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","group_chat_list":[{"chat_id":"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ","status":0},{"chat_id":"wrZw2MCQAAa8rgaMqdrlMfVL4YVw9t_w","status":0},{"chat_id":"wrZw2MCQAA3bb69dMZR0wmaHo-3XbdZg","status":0},{"chat_id":"wrZw2MCQAAMtrlDvtBidmx3ihQM147iw","status":0},{"chat_id":"wrZw2MCQAALnZ9Bn9cH9UF4CnhG_atFg","status":0}]},"time":1701246096}:941

        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"limit\":11,\"owner_filter\":{\"userid_list\": [\"YangLin\"]}}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","group_chat_list":[{"chat_id":"wrZw2MCQAAqG6Bwig6hePemDtHwh9ExQ","status":0}]},"time":1701246236}:887

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }

    public static void externalcontactBatch() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/externalcontact/batch";
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"limit\":11,\"userid_list\":[\"YangLin\"]}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","external_contact_list":[{"follow_info":{"userid":"YangLin","remark":"Mr.先生","description":"","createtime":1701238015,"tag_id":[],"remark_mobiles":[],"add_way":1,"oper_userid":"wmZw2MCQAArx3jAyw-OTjea0TET_AB8A"},"external_contact":{"external_userid":"wmZw2MCQAArx3jAyw-OTjea0TET_AB8A","name":"Mr.先生","type":1,"avatar":"http://wx.qlogo.cn/mmhead/ZHK9X5JK58wTicamlCQGvaMTPtJ7LVa03iar9HJsoC822sBOuKgCwrLA/0","gender":0}},{"follow_info":{"userid":"YangLin","remark":"芝麻沉","description":"","createtime":1701236961,"tag_id":[],"remark_mobiles":[],"add_way":1,"oper_userid":"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA"},"external_contact":{"external_userid":"wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA","name":"芝麻沉","type":1,"avatar":"http://wx.qlogo.cn/mmhead/0wRpPfN90ibDXImyms8U6rzicTY8CwAicWbI3EHiajdpW08ibfawRPt9zACyFMzib6HwbxlvufzicgkjzE/0","gender":2}}],"next_cursor":""},"time":1701245954}:978

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }


    public static void externalcontactList() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/externalcontact/list";
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"userid\":\"YangLin\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","external_userid":["wmZw2MCQAAJhG6TMk6sBFYHTHWKt7cPA","wmZw2MCQAArx3jAyw-OTjea0TET_AB8A"]},"time":1701245556}:1714

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }


    public static void sendMessage() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/message/send";
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"touser\":\"YangLin|LiuXinSheng\",\"msgtype\":\"text\",\"content\":{\"content\":\"你好，欢迎加入减脂营!\"}}");
        //{"code":0,"msg":"操作成功","data":"3yzdAQ63LCLTa8NCVqmn2aMkXarUDtTXAmG0XCqq7ffW8ZdVhelk4Sh-KcIa4qGCYIoNcAIbdN-zKQk0JbyrWA","time":1701237866}:846

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }
    public static void recallMessage() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/message/recall";
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"msgid\":\"YangLin\"}");

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }

    public static void getuser() {
        String uri = "https://twedata.gymcity.com.cn/message/qiwei/user/getuserid";
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"mobile\":\"13670279423\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","userid":"YangLin"},"time":1701237204}:767
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"mobile\":\"13572031750\"}");
        //{"code":0,"msg":"操作成功","data":{"errcode":0,"errmsg":"ok","userid":"LiuXinSheng"},"time":1701237498}:747
        JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"mobile\":\"15386435809\"}");

        //String uri = "https://twedata.gymcity.com.cn/message/qiwei/user/list";
        //JSONObject body = new JSONObject("{\"corpid\":\"wwbdeaa3266417c5e1\",\"agentId\":1000035,\"limit\":10}");

        String sign = MD5.create().digestHex(body+"#270ad09ccbec217ffe120a8550f61c12");
        JSONObject header = new JSONObject("{\"X-TRACEID\":\"uuid\",\"X-APPKEY\":\"17600048054\",\"X-SIGN\":\""+sign+"\"}");

        Long s = System.currentTimeMillis();
        System.out.println(HttpUtils.sendPost(uri,header,body)+":"+(System.currentTimeMillis()-s));

    }

    public static JSONObject recall() {
        String access_token = getAccessToken();
        JSONObject obj = new JSONObject();
        //obj.set("msgid", "mrVtVXE39it1tWVvd57npGh19Xu19xdT6b_AyzIEVO9h1H3gdJMBsF5jbBQyWDQUv2kGt5hJ9s9Pm_JfGq9v4w");
        obj.set("msgid", "mrVtVXE39it1tWVvd57npGh19Xu19xdT6b_AyzIEVO8Z_1CXjYwZXK35WLdLVyqhb2OAVhQZ77h8a79aGMhGcQ");
        JSONObject result = HttpUtils.sendPost("https://qyapi.weixin.qq.com/cgi-bin/message/recall?access_token="+access_token,new JSONObject(),obj);
        //{"errcode":0,"errmsg":"ok"}
        //{"errcode":0,"errmsg":"ok"}
        return result;
    }

    public static JSONObject send() {
        String access_token = getAccessToken();
        JSONObject obj = new JSONObject();
        obj.set("agentid", 1000002);
        obj.set("touser","YangLin_1");
        obj.set("msgtype","text");
        Map text = new HashMap();
        text.put("content","你好，速达云温馨提示");
        obj.set("text",text);
        JSONObject result = HttpUtils.sendPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token,new JSONObject(),obj);
        //{"errcode":0,"errmsg":"ok","msgid":"mrVtVXE39it1tWVvd57npGh19Xu19xdT6b_AyzIEVO9h1H3gdJMBsF5jbBQyWDQUv2kGt5hJ9s9Pm_JfGq9v4w"}
        //{"errcode":0,"errmsg":"ok","msgid":"mrVtVXE39it1tWVvd57npGh19Xu19xdT6b_AyzIEVO8Z_1CXjYwZXK35WLdLVyqhb2OAVhQZ77h8a79aGMhGcQ"}
        return result;
    }

    /**
     * @return
     */
    public static JSONObject list() {
        String access_token = getAccessToken();
        JSONObject obj = new JSONObject();
        //obj.set("cursor","");
        obj.set("limit", 11);
        JSONObject result = HttpUtils.sendPost("https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token="+access_token,new JSONObject(),obj);
        return result;
    }

    public static JSONObject getuserid() {
        String access_token = getAccessToken();
        JSONObject obj = new JSONObject();
        obj.set("mobile", "13670279423");
        JSONObject result = HttpUtils.sendPost("https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token="+access_token,new JSONObject(),obj);
        //{"errcode":0,"errmsg":"ok","userid":"YangLin_1"}
        return result;
    }

    /**
     * 3.获取access_token
     *
     * @return
     */
    public static String getAccessToken() {

        if("".equals("")){
            return "yyJRKvvA9mGqQcg2uMrbMyBrdPISrCk74Y0z37tyipXNTNIMdmx85KzieXiPpxfZfUi9lMWlINXSnwY1QmlCJ2lPSaYf6TmAUKyQcd9kiAHm63IxLZ4u8FnPV3bwCq3qFOaPEQkdvHgUQ9vb_UTa7eYQjkaETGBmKzUltX6Q8zyFD3IpLibXQkCQubnRfhEvQnAWS3qwAX_YiRUDs0DE4g";
        }

        String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww87e9692039981106&corpsecret=gpVmbjby2qKlPgWf0AHO8OBAQIinesSDh-Qkhag8ywE";
        JSONObject jsonObject = HttpUtils.doGet(requestUrl);
        //{"errcode":0,"errmsg":"ok","access_token":"yyJRKvvA9mGqQcg2uMrbMyBrdPISrCk74Y0z37tyipXNTNIMdmx85KzieXiPpxfZfUi9lMWlINXSnwY1QmlCJ2lPSaYf6TmAUKyQcd9kiAHm63IxLZ4u8FnPV3bwCq3qFOaPEQkdvHgUQ9vb_UTa7eYQjkaETGBmKzUltX6Q8zyFD3IpLibXQkCQubnRfhEvQnAWS3qwAX_YiRUDs0DE4g","expires_in":7200}
        // 如果请求成功
        if (null != jsonObject) {
            try {
                return jsonObject.getStr("access_token");
            } catch (Exception e) {
                // 获取token失败
                System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + ":" + jsonObject.getStr("errmsg"));
            }
        }
        return null;
    }

}
